<div id="kaoHeManager" style="height: 100%;width: 100%">
    <div  style="width:100%;text-align: right;background-color: #009E94;" >
        <input id="peiZhiCheJianManagerAssessMonth" data-options="label:'考核月份',labelWidth:60,width:200" type= "text" class= "easyui-datebox" required ="required">
        <a id="managerKaoheSubmit" class="easyui-linkbutton" style="width: 80px;background-color:#a1af1a" plain=true><i class="fa fa-check-circle fa-lg fa-fw"></i>提交</a>
    </div>
    <div style="height:93%;">
        <!--奖金表格-->
        <table id="peiZhiCheJianManager"></table>
    </div>
    <div id="peiZhiCheJianManagerToolbar" style="display: none;">
        <div style="width: 100px;float: left">
            <a class="easyui-linkbutton" data-options="plain:true" onclick="javaScript:$('#peiZhiCheJianManager').edatagrid('saveRow')"><i class="fa fa-question-circle fa-fw" aria-hidden="true"></i>说明</a>
        </div>
        <div style="width: 250px;float:right;text-align: right">
            <a class="easyui-linkbutton" data-options="plain:true" onclick="javaScript:$('#peiZhiCheJianManager').edatagrid('saveRow')"><i class="fa fa-floppy-o fa-fw" aria-hidden="true"></i>保存</a>
            <a class="easyui-linkbutton" data-options="plain:true">|</a>
            <a class="easyui-linkbutton" data-options="plain:true" onclick="javaScript:$('#peiZhiCheJianManager').edatagrid('destroyRow')"><i class="fa fa-trash fa-fw" aria-hidden="true"></i>删除</a>
        </div>
        <!--清除浮动-->
        <div style="clear: both"></div>
    </div>

</div>

<script th:inline="none">
    //当前编辑行的索引
    var peiZhiCheJianManagerIndex=0;

    //提交数据
    $("#managerKaoheSubmit").click(function () {
        //获取表中所有的数据
        var jxAssessPzcjManagers = $("#peiZhiCheJianManager").datagrid("getRows");

        //获取行尾'合计'
        var footerRows = $("#peiZhiCheJianManager").datagrid("getFooterRows");
        // 封装年月和部门---全局变量设定在jxAssessGlobalVariable的js文件中；
        jxAssessPzcjManagers.forEach(function (val) {
            val.assessYear=pzcj_currentYear;
            val.assessMonth=pzcj_currentMonth;
            val.assessDept=pzcj_currentDept;
        })
        var data=JSON.stringify({jxAssessPzcjs:jxAssessPzcjManagers,maps:footerRows});
        //提交数据
        var result = commonAjax("post","/jx/pzcj/addManagersAssessData",data,"application/json;charset=utf-8");
        if (result.status==200){
            $("#peiZhiCheJianManager").datagrid("loadData",{total: 0, rows: [],footer:[]});
            windowsOperation(null,null,result)
        }else {
            layer.msg(result.msg,{"icon":5})
        }
    })

    //日期选择事件
    $("#peiZhiCheJianManagerAssessMonth").datebox({
        onSelect: function(date){
            $("#peiZhiCheJianManager").datagrid("loadData",{total: 0, rows: [],footer:[]});
            var currentYear=date.getFullYear()
            var currentMonth=date.getMonth()+1
            managerInit(currentYear,currentMonth)
            addPZCJData("peiZhiCheJianManager",window.JX_PZCJ_KAILIAOGUANLIRENYUAN,"1002O110000000000MMB",currentYear,currentMonth)
        }
    })
    var date= new Date();
    managerInit(date.getFullYear(),date.getMonth()+1)
    //绩效考核表
    function managerInit(currentYear,currentMonth) {

        window.pzcj_currentYear = currentYear;//年
        window.pzcj_currentMonth = currentMonth; //月
        var currentYM=currentYear+"年"+currentMonth+"月";
        var managerColumns = {
            init: [//表头
                [
                    {"title": ' <span style="font-size: 18px">生产部绩效考核统计表（组长、主管综合绩效奖）</span><span style="color: #009E94">绩效奖金标准*出勤率-质量要求扣款-5S及目视化管理扣款-日常检查扣款</span>',"align": "center", "colspan":15},

                ],
                [  {"title": '考核部门（科室）',"align": "center", "colspan":3},
                    {"field": 'id3', "title": "生产部/配制车间","align": "center", "colspan":2},
                    {"title": '', "align": "center","colspan":5},
                    {"title": '考核周期',"align": "center"},
                    {"field": 'id2', "title": currentYM, "align": "center","colspan":3},
                    {"field": 'resultMoney', "title": '最终综合<br/>绩效（元）', "width": 30,"rowspan":2,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                ],
                [
                    {"field": 'id', "title": 'ID', "width": 0, "align": "center", "hidden": "true"},
                    {"field": 'empName', "title": '姓名', "width": 30, "align": "center"},
                    {"field": 'empCode', "title": '工号', "width": 30, "align": "center"},
                    {"field": 'postName',"title": '岗位',"width": 30,"align": "center",
                        "editor":{"type":"textbox",options:{"readonly":true}}
                    },
                    {"field": 'empGroup', "title": '班组', "width": 30, "align": "center"},
                    {"field": 'teamEfficiency', "title": '班组生产<br/>效率', "width": 30, "align": "center",
                        "editor": {"type": "textbox", "options": {"onChange":peiZhiCheJianManagerTeamEfficiency}}},
                    {"field": 'teamTotalBonus',"title": '班组总绩效<br/>奖金',"width": 30,"align": "center"},
                    {"field": 'teamAvgBonus',"title": '平均绩效<br/>奖金',"width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                    {"field": 'performanceBonus',"title": '绩效奖金<br/>标准',"width": 30,"align": "center",
                        "editor": {"type": "textbox", "options": {}}},
                    {"field": 'penaltyQuality', "title": '质量要求<br/>扣款', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":peiZhiCheJianManagerResultMoney}}},
                    {"field": 'score5s', "title": '5S及目视化<br/>管理得分', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":peizhiCheJianManagerScore5s}}},
                    {"field": 'penalty5s', "title": '5S及目视化<br/>管理扣款', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                    {"field": 'penaltyEveryday', "title": '日常检查<br/>扣款', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":peiZhiCheJianManagerResultMoney}}},
                    {"field": 'attendanceRate', "title": '出勤率(%)', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":peiZhiCheJianManagerResultMoney}}},
                ]]

        }

        //奖金基数表格
        $("#peiZhiCheJianManager").edatagrid({
            nowrap: false,
            columns: managerColumns.init,
            showFooter: true,
            autoSave:true,
            fitColumns: true,
            scrollbarSize:0,  //最右侧多出来的去除
            toolbar:"#peiZhiCheJianManagerToolbar",
            onBeforeEdit:function (index, row) { //编辑器打开触发事件
                peiZhiCheJianManagerIndex=index;//保存当前索引

            },

            onAfterEdit:function (index, row) {
                //console.log("在用户完成编辑一行的时候触发:统计最终综合绩效")
                totalpeiZhiCheJianManager();
            },
            onDestroy:function (index,row) { //删除一行触发事件
                totalpeiZhiCheJianManager();
            }

        })
    }

    //最终绩效奖金总合计
    function totalpeiZhiCheJianManager() {
        //console.log("统计全部--最终绩效奖金总合计")
        //统计
        var rows = $("#peiZhiCheJianManager").datagrid("getRows");
        var  result=0;
        rows.forEach(function (row) {
            var val=Number(row.resultMoney)
            if (val){
                result+=val
            }

        })

        //添加页脚行方法
        $("#peiZhiCheJianManager").edatagrid('reloadFooter', [{
            empCode: "合计", resultMoney: fomatFloat(result,2)
        }])
    }

    //统计个人--最终综合绩效:
    function peiZhiCheJianManagerResultMoney(newValue,oldValue) {
        //console.log("统计个人--最终综合绩效")
        var resultMoney = getEditObject("resultMoney");         //获取最终绩效奖金
        var performanceBonus = getEditObject("performanceBonus").textbox("getValue"); //绩效奖金标准
        performanceBonus=performanceBonus?Number(performanceBonus):0;
        var penaltyQuality = getEditObject("penaltyQuality").textbox("getValue");     //质量要求扣款
        penaltyQuality=penaltyQuality?Number(penaltyQuality):0;
        var penalty5s = getEditObject("penalty5s").textbox("getValue");               //5S及目视化管理扣款
        penalty5s=penalty5s?Number(penalty5s):0;
        var penaltyEveryday = getEditObject("penaltyEveryday").textbox("getValue");     //日常检查扣款
        penaltyEveryday=penaltyEveryday?Number(penaltyEveryday):0;
        var attendanceRate = getEditObject("attendanceRate").textbox("getValue");     //出勤率
        attendanceRate=attendanceRate?Number(attendanceRate):0;
        //统计最终综合绩效:绩效奖金标准*出勤率-质量要求扣款-5S及目视化管理扣款-日常检查扣款
        resultMoney.textbox("setValue", fomatFloat(performanceBonus * attendanceRate/100- penaltyQuality - penalty5s - penaltyEveryday, 2))
    }

    //班组生产效率事件
    function peiZhiCheJianManagerTeamEfficiency(newValue,oldValue){
        //console.log("班组生产效率事件")
       var performanceBonus= getEditObject("performanceBonus")
        if (newValue<95 ||newValue>103){
            performanceBonus.textbox("setValue",0); //绩效奖金标准

        }else {
            var teamAvgBonus = getEditObject("teamAvgBonus").textbox("getValue"); //平均绩效奖金
            var postName = getEditObject("postName").textbox("getValue"); //postName
            if (postName==window.JX_PZCJ_ZHUGUAN){
                performanceBonus.textbox("setValue",fomatFloat(teamAvgBonus*1.4,2));
            }else if (postName==window.JX_PZCJ_JISHUZUZHANG){
                performanceBonus.textbox("setValue",fomatFloat(teamAvgBonus*1.2,2));
            }
        }
        peiZhiCheJianManagerResultMoney();
    }

    //5S及目视化管理得分触发事件:当月配制车间5S检查低于85分，扣减奖金50元。
    function peizhiCheJianManagerScore5s(newValue,oldValue) {
        if (newValue){
            var penalty5s = getEditObject("penalty5s");               //5S及目视化管理扣款
            if (newValue<85){
                penalty5s.textbox("setValue",50);
            }else {
                penalty5s.textbox("setValue",0);
            }
        }

        peiZhiCheJianManagerResultMoney();
    }

    //获取当前编辑行指定的jquery对象
    function getEditObject(fieldValue) {
        var j = $('#peiZhiCheJianManager').datagrid('getEditor', {
            index: peiZhiCheJianManagerIndex,
            field: fieldValue
        }).target;
        return j
    }
</script>
